<template>
  <div class="app-container">
    <el-card class="detail-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <el-button icon="ArrowLeft" @click="goBack">返回</el-button>
          <span class="card-title">工单详情</span>
        </div>
      </template>

      <div class="detail-content">
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="info-section">
              <h3 class="section-title">基本信息</h3>
              <el-descriptions :column="1" border>
                <el-descriptions-item label="工单编号">{{ workorder.id }}</el-descriptions-item>
                <el-descriptions-item label="工单类型">
                  <el-tag :type="getTypeTagType(workorder.type)" size="small">
                    {{ getTypeLabel(workorder.type) }}
                  </el-tag>
                </el-descriptions-item>
                <el-descriptions-item label="工单状态">
                  <el-tag :type="getStatusTagType(workorder.status)" size="small">
                    {{ getStatusLabel(workorder.status) }}
                  </el-tag>
                </el-descriptions-item>
                <el-descriptions-item label="设备ID">{{ workorder.deviceId }}</el-descriptions-item>
                <el-descriptions-item label="站点ID">{{ workorder.siteId }}</el-descriptions-item>
                <el-descriptions-item label="创建时间">
                  {{ formatDateTime(workorder.createdAt) }}
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </el-col>
          
          <el-col :span="12">
            <div class="info-section">
              <h3 class="section-title">处理信息</h3>
              <el-descriptions :column="1" border>
                <el-descriptions-item label="创建人ID">{{ workorder.creatorId }}</el-descriptions-item>
                <el-descriptions-item v-if="workorder.assigneeId" label="指派人ID">
                  {{ workorder.assigneeId }}
                </el-descriptions-item>
                <el-descriptions-item v-if="workorder.evaluation" label="用户评价">
                  {{ workorder.evaluation }}
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </el-col>
        </el-row>

        <div class="info-section">
          <h3 class="section-title">工单描述</h3>
          <el-card class="description-card" shadow="never">
            <p class="description-text">{{ workorder.description || '暂无描述' }}</p>
          </el-card>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'
import WorkOrderAPI from '@/api/system/workorder.api'

const route = useRoute()
const router = useRouter()
const workorder = ref({})

const goBack = () => {
  router.go(-1)
}

const loadWorkorder = async () => {
  try {
    const id = route.params.id
    const result = await WorkOrderAPI.getById(id)
    workorder.value = result
  } catch (error) {
    ElMessage.error('获取工单详情失败')
  }
}

const getTypeLabel = (type) => {
  const typeMap = {
    'device_failure': '设备故障',
    'water_leakage': '漏水问题',
    'meter_malfunction': '水表故障',
    'pipeline_issue': '管道问题',
    'other': '其他问题'
  }
  return typeMap[type] || type
}

const getTypeTagType = (type) => {
  const typeMap = {
    'device_failure': 'danger',
    'water_leakage': 'warning',
    'meter_malfunction': 'info',
    'pipeline_issue': 'error',
    'other': ''
  }
  return typeMap[type] || ''
}

const getStatusLabel = (status) => {
  const statusMap = {
    'pending': '待处理',
    'assigned': '已派发',
    'in_progress': '处理中',
    'completed': '已完成',
    'cancelled': '已取消'
  }
  return statusMap[status] || status
}

const getStatusTagType = (status) => {
  const statusMap = {
    'pending': 'warning',
    'assigned': 'info',
    'in_progress': 'primary',
    'completed': 'success',
    'cancelled': 'danger'
  }
  return statusMap[status] || ''
}

const formatDateTime = (dateTime) => {
  if (!dateTime) return '-'
  return new Date(dateTime).toLocaleString('zh-CN')
}

onMounted(() => {
  loadWorkorder()
})
</script>

<style scoped>
.app-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 84px);
}

.detail-card {
  border-radius: 12px;
  border: none;
}

.card-header {
  display: flex;
  align-items: center;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-left: 20px;
}

.detail-content {
  padding: 20px 0;
}

.info-section {
  margin-bottom: 30px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 15px;
  padding-left: 10px;
  border-left: 4px solid #409eff;
}

.description-card {
  background: #f8f9fa;
  border: 1px solid #e9ecef;
}

.description-text {
  margin: 0;
  line-height: 1.6;
  color: #606266;
  font-size: 14px;
}

:deep(.el-card) {
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

:deep(.el-button) {
  border-radius: 6px;
}
</style>
